package com.example.smartcommunity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.smartcommunity.com.example.utils.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class RegisteredActivity extends AppCompatActivity {
    private EditText et_username, et_password, et_phone,
            et_verification;
    private Button btn_registered,btn_verification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_phone = findViewById(R.id.et_phone);
        et_verification = findViewById(R.id.et_verification);
        btn_registered = findViewById(R.id.btn_registered);

        btn_registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户名和密码
                String et_usernameStr = et_username.getText().toString().trim();
                String et_passwordStr = et_password.getText().toString().trim();
                String et_phonedStr = et_phone.getText().toString().trim();
                String et_verificationStr = et_verification.getText().toString().trim();
                //判断用户名和密码
                if (TextUtils.isEmpty(et_usernameStr)) {
                    Toast.makeText(RegisteredActivity.this, "输入用户名", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(et_passwordStr)) {
                    Toast.makeText(RegisteredActivity.this, "输入密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(et_phonedStr)) {
                    Toast.makeText(RegisteredActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(et_verificationStr)){
                    Toast.makeText(RegisteredActivity.this, "输入验证码", Toast.LENGTH_SHORT).show();
                } else {
                    Map<String, Object> map = new HashMap<>();
                    OkHttpUtils.getDataFromIntent("regiestbyusernamepassword?username=" + et_usernameStr + "&password=" + et_passwordStr + "&phone" + et_phonedStr, map, new OkHttpUtils.CallBack() {
                        @Override
                        public void successful(String string) {
                            try {
                                JSONObject jsonObject = new JSONObject(string);
                                String msg = jsonObject.getString("msg");

                                if (msg.equals("新增失败")) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(), "注册错误", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                } else {
                                    Toast.makeText(RegisteredActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(RegisteredActivity.this, LoginActivity.class));
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            RegisteredActivity.this.finish();

                        }

                        @Override
                        public void onErrer(Exception e) {
                            Log.e("请求错误", e.getMessage());
                        }
                    });
                }
            }
        });
    }
}

